[contents] [prev] [top] (5 out of 5)

Creating Exception Subclasses and Instances

To define subclasses of Exception or any of its subclasses, use the class construct as discussed in Chapter 6, "Defining Classes and Objects." For example, the following class declaration creates a class of exceptions that is particular to a class of objects called People:

class PeopleException (Exception) end

To create instances of exceptions, use new with appropriate exception class. The syntax for using new on exception classes is:

new ExceptionClass name: "name" format: "format string"
The class ExceptionClass is the class to instantiate. The top-level exception class is Exception. The value in name is a string containing the name of the exception instance, for example "tooOld".The value in format string is the format string for the exception. The string is used as the default exception message, so it should contain useful information about the exception. The format string follows the substitution character rules as described in "Formatted Output" on page 76.

When an exception is reported, the second argument to the report method is passed to the format string for use by the substitution characters in the string. When calling report on an exception, make sure that the second argument matches the type of input required by the exception's format string. If the format string contains %*, the second argument to report should be an object. If the format string contains %n, the second argument to report should be a LinearCollection object containing at least n objects.

The following statement creates a tooOld exception as an instance of the exception class PeopleException.

tooOld := new PeopleException name: "tooOld" \
		format: "No one lives to be %* years old."

When this exception is reported, the second argument to report should be the unsuitable age. For example, the People class defines an ageSetter method that reports the tooOld exception if the age is greater than 115:

class People ()
instance variables _age
instance methods
method ageGetter self ->
return self._age
		method ageSetter self val -> (
if val > 115 then report tooOld val
else self._age := val
)
end

If a script attempts to set a person's age to a value that is too large, ageSetter calls report on the tooOld instance of PeopleException, with an argument of 122. This reports an exception:

abuela := new People
abuela.age := 122
-- ** No one lives to be 122 years old. (tooOld)

The default message is usually printed when the exception is reported but not caught. If the exception is caught by the catch list in a guard construct, the consequences of the exception depend entirely on the action defined by the clause in the catch list that catches the exception (which could include printing out the format string for the exception).


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.